package com.getpebble.android.jsbridge;

import android.text.TextUtils;
import android.webkit.WebView;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.LockerApp;
import com.google.gson.Gson;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.IllegalFormatException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class JsBridgeUtil {
    public static final String TAG = JsBridgeUtil.class.getSimpleName();

    public static Map<String, String> getQueryParams(String url) {
        Map<String, String> params = new HashMap();
        url.replace("pebble-method-call-js-frame://", "");
        try {
            for (String keyValuePair : URLDecoder.decode(url, "UTF-8").split("&", 2)) {
                String[] keyValue = keyValuePair.split("=", 2);
                if (keyValue.length == 2) {
                    params.put(keyValue[0], keyValue[1]);
                }
            }
        } catch (UnsupportedEncodingException e) {
            Trace.error(TAG, "Failed to decode url: " + url, e);
        }
        return params;
    }

    public static void notifyLoadResult(WebView webView, boolean result, JSONObject methodArgs) {
        String requestId = methodArgs.optString("callbackId", null);
        appendResultToDataArgs(methodArgs, "execution_result", Boolean.valueOf(result));
        if (!TextUtils.isEmpty(requestId)) {
            try {
                webView.loadUrl(String.format("javascript:PebbleBridge.handleResponse(%s);", new Object[]{methodArgs.toString()}));
            } catch (IllegalFormatException ife) {
                Trace.error(TAG, "Failed to notifyLoadResult.", ife);
            }
        }
    }

    public static void appendResultToDataArgs(JSONObject methodArgsJson, String key, Object value) {
        if (methodArgsJson != null && !TextUtils.isEmpty(key)) {
            try {
                JSONObject data = methodArgsJson.optJSONObject("data");
                if (data == null) {
                    data = new JSONObject();
                    methodArgsJson.put("data", data);
                }
                data.put(key, value);
            } catch (JSONException e) {
                Trace.error(TAG, "appendResultToMethodArgs - JSONException:", e);
            }
        }
    }

    public static String getTitle(JSONObject methodArgs) {
        String title = null;
        if (methodArgs != null) {
            try {
                title = methodArgs.getJSONObject("data").optString("title", null);
            } catch (JSONException e) {
                Trace.error(TAG, "handleSetNavBarTitleRequest - JSONException while parsing:" + methodArgs, e);
            }
        }
        return title;
    }

    public static boolean getShowSearch(JSONObject methodArgs) {
        boolean showSearch = false;
        if (methodArgs != null) {
            try {
                showSearch = methodArgs.getJSONObject("data").optBoolean("show_search", true);
            } catch (JSONException e) {
                Trace.error(TAG, "handleSetNavBarTitleRequest - JSONException while parsing:" + methodArgs, e);
            }
        }
        return showSearch;
    }

    public static LockerApp getWatchAppFromMethodArgs(JSONObject methodArgs) {
        LockerApp lockerApp = null;
        try {
            return (LockerApp) new Gson().fromJson(methodArgs.getJSONObject("data").toString(), LockerApp.class);
        } catch (JSONException e) {
            Trace.error(TAG, "Failed to parse watchapp from methodArgs: " + methodArgs.toString(), e);
            return lockerApp;
        }
    }
}
